﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mani.Common;
using Mani.Common.Security;

namespace RastinArgham.CRM.Web
{
    public partial class roles : System.Web.UI.Page
    {
        public ISecurityManager securityManager { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (securityManager == null)
            {
                securityManager = new SecurityManager();
                securityManager.authProvider = new WebAuthorizationProvider();
            }

            

            if (!securityManager.IsActionPermitted("user_management"))
                Response.Redirect("~/access_denied.aspx");

            if (!Page.IsPostBack)
            {
                LoadActions();
                LoadRoles();
            }
        }

        private void LoadRoles()
        {
            gvRoles.DataSource = securityManager.GetRoles();
            gvRoles.DataBind();
        }

        private void LoadActions()
        {
            cbRoleActions.DataSource = securityManager.GetActions();
            cbRoleActions.DataBind();
        }

        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            if (txtRoleName.ReadOnly)
                Update();
            else
                New();

            ClearForm();
            LoadRoles();
            UIHelper.ClientAlert(this, "تغییرات با موفقیت ذخیره شد");
        }

        private void Update()
        {
            aspnet_Roles role = securityManager.GetRoleByName(txtRoleName.Text);

            foreach (ListItem item in cbRoleActions.Items)
            {
                Mani.Common.Security.Action action = securityManager.GetActionByName(item.Value);

                if (item.Selected)
                {
                    if (role.Actions.Where( a => a.ActionName.ToLower() == item.Value.ToLower() ).Count() == 0)
                        securityManager.AddActionToRole(action, role);
                }
                else if (role.Actions.Where(a => a.ActionName.ToLower() == item.Value.ToLower()).Count() > 0)
                {
                    securityManager.RemoveActionFromRole(action, role);
                }
            }
        }

        private void New()
        {
            aspnet_Roles role = securityManager.CreateRole(txtRoleName.Text);

            foreach (ListItem item in cbRoleActions.Items)
            {
                if (item.Selected)
                    securityManager.AddActionToRole(securityManager.GetActionByName(item.Value), role);
            }
        }

        protected void gvRoles_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
        }

        protected void gvRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string roleName = gvRoles.DataKeys[e.RowIndex].Value.ToString();
            securityManager.DeleteRole(roleName);
            LoadRoles();
            ClearForm();
        }

        protected void gvRoles_RowCreated(object sender, GridViewRowEventArgs e)
        {
            UIHelper.GridDeleteConfirm(e.Row);
        }

        protected void gvRoles_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            ClearForm();

            string roleName = gvRoles.DataKeys[e.NewSelectedIndex].Value.ToString();
            aspnet_Roles role = securityManager.GetRoleByName(roleName);

            txtRoleName.Text = role.RoleName;
            txtRoleName.ReadOnly = true;

            foreach (Mani.Common.Security.Action a in role.Actions)
            {
                ListItem item = cbRoleActions.Items.FindByValue(a.ActionName);
                if ( item != null ) 
                    item.Selected = true;
            }
        }

        private void ClearForm()
        {
            txtRoleName.Text = String.Empty;
            txtRoleName.ReadOnly = false;
            cbRoleActions.ClearSelection();
        }
    }
}